package com.dzm.xgb.core.controller.api;


import com.dzm.common.result.R;
import com.dzm.common.util.JwtUtils;
import com.dzm.xgb.core.hfb.RequestHelper;
import com.dzm.xgb.core.pojo.entity.LendItem;
import com.dzm.xgb.core.pojo.vo.InvestVO;
import com.dzm.xgb.core.service.LendItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Dzm
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/lendItem")
public class LendItemController {

    @Autowired
    private LendItemService lendItemService;

    /**
     * 投标提交
     * @param vo
     * @param token
     * @return
     */
    @PostMapping("/auth/commitInvest")
    public R commitInvest(@RequestBody InvestVO vo, @RequestHeader("token") String token) {
        Long userId = JwtUtils.getUserId(token);
        String userName = JwtUtils.getUserName(token);

        //将userId于username与vo绑定
        vo.setInvestUserId(userId);
        vo.setInvestName(userName);

        //生产表单
        String formStr = lendItemService.commitInvest(vo);

        return R.ok().data("formStr", formStr);
    }

    /**
     * 投标异步回调
     * @param request
     * @return
     */
    @PostMapping("/notify")
    public String notify(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        //验签
        if(RequestHelper.isSignEquals(map)) {
            if("0001".equals(map.get("resultCode").toString())) {

                return lendItemService.lendItemNotify(map);
            }else {
                return "fail";
            }
        }

        return "非法访问！";
    }

    @GetMapping("/list/{id}")
    public R list(@PathVariable("id") Long id) {
        List<LendItem> list = lendItemService.getLendItemList(id);

        return R.ok().data("list", list);
    }

}

